package org.fuys.coder.infrastructure.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.fuys.coder.domain.category.model.vo.CategoryVO;
import org.fuys.coder.domain.category.respository.ICategoryRepository;
import org.fuys.coder.infrastructure.dao.CategoryMapper;
import org.fuys.coder.infrastructure.model.po.Category;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.infrastructure.repository
 * @className: CategoryRepositoryImpl
 * @author: WangJing
 * @description: 分类仓储实现类
 * @date: 2024/6/25 17:49
 * @version: 1.0
 */
@Service
public class CategoryRepositoryImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryRepository {

    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryVO> getAllCategory() {
        final List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .select(Category::getId, Category::getCategoryName,Category::getCategoryDesc));
        return categories.stream().map(category -> new CategoryVO(category.getId(), category.getCategoryName(),category.getCategoryDesc())).collect(Collectors.toList());
    }

    @Override
    public List<Integer> getAllCategoryId() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>().select(Category::getId))
                .stream().map(Category::getId).collect(Collectors.toList());
    }

    @Override
    public List<CategoryVO> getCategoryPage(Integer curPage, Integer pageNum, String keywords) {
        Page<Category> page=new Page<>(curPage,pageNum);
        final Page<Category> categoryPage = categoryMapper.selectPage(page, new LambdaQueryWrapper<Category>()
                .like(Category::getCategoryName, keywords).like(Category::getCategoryDesc, keywords));
        if(ObjectUtils.isEmpty(categoryPage)){
            return null;
        }
        final List<Category> records = categoryPage.getRecords();
        if(ObjectUtils.isEmpty(records)){
            return null;
        }
        return records.stream().map(item -> {
            CategoryVO categoryVO = new CategoryVO();
            categoryVO.setId(item.getId());
            categoryVO.setName(item.getCategoryName());
            categoryVO.setDescription(item.getCategoryDesc());
            return categoryVO;
        }).collect(Collectors.toList());
    }
}
